#include "Tensor.h"


Tensor::Tensor(int size)
{
	x=size;
	y=size;
	z=size;
}


Tensor::~Tensor()
{
	
}


bool Tensor::setElement(int xx,int yy,int zz,double value)
{
	if(xx>=x) return false;
	if(yy>=y) return false;
	if(zz>=z) return false;
	//theTensor[xx][yy][zz] = value;
	double *tmp=new double[4];
	tmp[0]=xx;tmp[1]=yy;tmp[2]=zz;tmp[3]=value;
	theTensor.push_back(tmp);
	return true;
}
/*
/////////////////
bool less_second1(const double *a, const double *b){
	if(a[0]==b[0]){
		if(a[1]==b[1]){
			return a[2]<b[2];
		}
		else return a[1]<b[1];
	}
	else return a[0]<b[0];
}

void Tensor::Sort1(){
	sort(theTensor.begin(),theTensor.end(),less_second1);
}
////////////////////
bool less_second2(const double *a, const double *b){
	if(a[1]==b[1]){
		if(a[2]==b[2]){
			return a[0]<b[0];
		}
		else return a[2]<b[2];
	}
	else return a[1]<b[1];
}

void Tensor::Sort2(){
	sort(theTensor.begin(),theTensor.end(),less_second2);
}
///////////////////

bool less_second3(const double *a, const double *b){
	if(a[2]==b[2]){
		if(a[1]==b[1]){
			return a[0]<b[0];
		}
		else return a[1]<b[1];
	}
	else return a[2]<b[2];
}

void Tensor::Sort3(){
	sort(theTensor.begin(),theTensor.end(),less_second3);
}
////////////////////////
*/
SparseMatrix Tensor::matricification(int mode)
{
	
	if(mode == 1)
	{
	//	Sort1();
		
		SparseMatrix result(x,(long long)y*(long long)z);
		
		for(int i = 0; i < theTensor.size(); i++)
		{
			result.setElement(theTensor[i][0],theTensor[i][1]*z+theTensor[i][2],theTensor[i][3]);
		}
		//result.Sort();
		
		return result;
	}
	else if(mode == 2)
	{
	//	Sort2();
		SparseMatrix result(y,(long long)x*(long long)z);
		for(int i = 0; i < theTensor.size(); i++)
		{
			result.setElement(theTensor[i][1],theTensor[i][2]*x+theTensor[i][0],theTensor[i][3]);
		}
		//result.Sort();
		return result;
	}

	else if(mode ==3)
	{
	//	Sort3();
	//	cout<<"fuck1"<<endl;
		SparseMatrix result(z,(long long)x*(long long)y);
	//	cout<<"fuck2"<<endl;
		for(int i = 0; i < theTensor.size(); i++)
		{
			result.setElement(theTensor[i][2],theTensor[i][0]*y+theTensor[i][1],theTensor[i][3]);
		}
		//result.Sort();
	//	cout<<"fuck3"<<endl;
		return result;
	}

	
	//{
		//MatrixObject empty(0,0);
		//empty.matrixFailure = true;
		//return empty;
	//}
}



